//
//  WSJSportDataRecordViewController.m
//  RunningMan
//
//  Created by dllo on 16/5/23.
//  Copyright © 2016年 WSJ. All rights reserved.
//

#import "WSJSportDataRecordViewController.h"
#import "WSJSportDataRecordHeaderView.h"
#import "WSJSportDataRecordTableViewCell.h"
#import "RMDataModelTool.h"
#import "RMDataModel.h"
#import "WSJSportDataDetailViewController.h"
@interface WSJSportDataRecordViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, retain)UITableView *dataTabelView;

@property(nonatomic, retain)NSMutableArray *dataArr;
@property(nonatomic, retain)RMCoreData *coreDataManager;
@property(nonatomic, retain)NSMutableArray *modelArr;
@end

@implementation WSJSportDataRecordViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    
    //self.navigationController.navigationBar.barTintColor = [UIColor greenColor];
    
    self.coreDataManager = [RMCoreData defaultCoreDataManager];
    
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"RunData" inManagedObjectContext:self.coreDataManager.managedObjectContext];
    [fetchRequest setEntity:entity];
    
    NSError *error = nil;
    NSArray *fetchedObjects = [self.coreDataManager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    
    self.dataArr = [fetchedObjects mutableCopy];
    
    
    self.modelArr = [RMDataModelTool getArray];
    
    
    
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    //self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
}




- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"历史记录";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStyleDone target:self action:@selector(delegateAction)];
    self.dataArr = [NSMutableArray array];
    [self creatTableView];
    
}

-(void)creatTableView{
    
    self.dataTabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    self.dataTabelView.dataSource = self;
    self.dataTabelView.delegate = self;
    [self.view addSubview:self.dataTabelView];
    
    self.dataTabelView.sectionHeaderHeight = 50;
    self.dataTabelView.rowHeight = 130;
    [self.dataTabelView registerClass:[WSJSportDataRecordHeaderView class] forHeaderFooterViewReuseIdentifier:@"headerView"];
    [self.dataTabelView registerClass:[WSJSportDataRecordTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WSJSportDataRecordTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.dataModel = self.dataArr[indexPath.row];
    
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    WSJSportDataRecordHeaderView *header = [[WSJSportDataRecordHeaderView alloc] initWithReuseIdentifier:@"headerView"];
    return header;
    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WSJSportDataDetailViewController *detail = [[WSJSportDataDetailViewController alloc] init];
    
    detail.model = self.modelArr[indexPath.row];
    [self.navigationController pushViewController:detail animated:YES];
    
}








-(void)delegateAction{

    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否要删除全部记录" preferredStyle:1];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            for (RunData *data in self.dataArr) {
    
           [self.coreDataManager.managedObjectContext deleteObject:data];
    }
           [self.coreDataManager saveContext];
   
        
        [RMDataModelTool cancelRMDataModelInPlist];
        [self.modelArr removeAllObjects];
        
        
        
            [self.dataArr removeAllObjects];
            [self.dataTabelView reloadData];
        
    }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self dismissViewControllerAnimated:YES completion:^{
        }];
    }]];
    
    [self presentViewController:alert animated:YES completion:^{
    
      }];
    
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
